package com.zlll.winner.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zlll.winner.business.model.media.MediaNewList;
import com.zlll.winner.business.service.common.IRedisService;
import com.zlll.winner.business.service.media.IMediaListenUserLogService;
import com.zlll.winner.business.service.media.IMediaNewChatRecordsService;
import com.zlll.winner.business.service.media.IMediaNewListService;
import com.zlll.winner.business.vo.media.MediaNewListVo;
import com.zlll.winner.common.BaseController;
import com.zlll.winner.enums.media.MediaEnum;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("api/media/chat")
@RestController
public class MediaChatController extends BaseController {


    @Reference(version = "${dubbo.consumer.version}")
    private IMediaNewListService mediaNewListService;

    @Reference(version = "${dubbo.consumer.version}")
    private IMediaNewChatRecordsService chatRecordsService;

    @Reference(version = "${dubbo.consumer.version}")
    private IRedisService redisService;

    /**
     * 添加聊天记录
     * @param params
     * @return
     */
    @PostMapping("add")
    public Object addChatRecord(@RequestBody JSONObject params){
        return chatRecordsService.addChatRecord(params);
    }

    /**
     * 获取直播间聊天内容
     * @param params
     * @return
     */
    @PostMapping("living/records")
    public Object getLivingChartRecords(@RequestBody JSONObject params){
//        Long versionNo = params.getLong("versionNo");
        Long bizId = params.getLong("bizId");
        if(bizId == null || bizId == -1L){
            return chatRecordsService.getThreeHourChatRecords(params);
        }
        return chatRecordsService.getNewListChatRecords(params);
    }

    /**
     * 刷新直播间聊天内容
     * @param params
     * @return
     */
    @PostMapping("living/refresh/records")
    public Object refreshChatRecords(@RequestBody JSONObject params){
        Long lastTime = params.getLong("lastTime");
        Long bizId = params.getLong("bizId");
        Integer bizType = params.getInteger("bizType");
        Object result = new Object();
        if(bizId == null && bizType.equals(MediaEnum.TypeEnum.NEWLIST.val())){
            result =  chatRecordsService.refreshChatRecords(params);
        }else if(bizId != null && bizType.equals(MediaEnum.TypeEnum.NEWLIST.val())){
            result = chatRecordsService.refreshAudioChatRecords(params);
        }else if(bizId != null && bizType.equals(MediaEnum.TypeEnum.VIDEOLIST.val())){
            result = chatRecordsService.refreshVideoChatRecords(params);
        }
        return result;
    }

    /**
     * 获取版本号
     * @return
     */
    @PostMapping("living/records/version")
    public Object getVersion(){
        String version = redisService.getRedisByString("living.refresh.version");
        JSONObject result = new JSONObject();
        result.put("version",version);
        return result;
    }

}


